﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PTTK_HTTT_06.BUS;
using System.Data;
using System.Data.SqlClient;

namespace PTTK_HTTT_06.DAO
{
    class MON_AN_DB
    {
        #region public static List<MON_AN> LayDanhSach()
        public static List<MON_AN> LayDanhSach()
        {
            DataProvider dataProvider = new DataProvider();
            string sqlStr = "select * from MON_AN";
            if (!dataProvider.IsConnect())
                dataProvider.Connect();
            DataTable dt = dataProvider.ExcuteQueryDataTable(sqlStr);
            dataProvider.Disconnect();

            if (dt != null && dt.Rows.Count <= 0)
                return null;

            List<MON_AN> monAnList = new List<MON_AN>();
            for (int i = 0, count = dt.Rows.Count; i < count; i++)
            {
                MON_AN ma = new MON_AN();
                ma.MaMonAn = int.Parse(dt.Rows[i]["MaMonAn"].ToString());
                ma.TenMonAn = dt.Rows[i]["TenMonAn"].ToString();

                monAnList.Add(ma);
            }

            return monAnList;
        }
        #endregion

        internal static void ThemMonAn(MON_AN _nguyenLieu)
        {
            DataProvider _provider = new DataProvider();
            _provider.Connect();
            SqlParameter[] _para = 
            {
                new SqlParameter("@TenMonAn",SqlDbType.NVarChar,50){Value = _nguyenLieu.TenMonAn}
            };
            _provider.ExcuteProcedure("usp_ThemMonAn", _para);
            _provider.Disconnect();        
        }

        internal static int LayMaMonAnTheoTen(string p)
        {
            DataProvider _provider = new DataProvider();
            _provider.Connect();
            string sql = "select MaMonAn from MON_AN where TenMonAn like N'"+ p +"'";
            DataTable dt = _provider.ExcuteQueryDataTable(sql);
            _provider.Disconnect();
            return int.Parse(dt.Rows[0][0].ToString());
        }
    }
}
